package com.sk.sourcecircle.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.activity.mine.act.PreviewViewpagerActivity;
import com.sk.sourcecircle.entity.AlbumaBean;
import com.sk.sourcecircle.entity.CollectListBean;
import com.sk.sourcecircle.utils.MyUtils;
import com.sk.sourcecircle.view.FlowLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by sk on 2017/12/29.
 */

public class MyCollectionAdapter extends BaseMultiItemQuickAdapter<CollectListBean.DataBeanX, BaseViewHolder> {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public MyCollectionAdapter(List<CollectListBean.DataBeanX> data) {
        super(data);
        addItemType(1, R.layout.item_collect_event);
        addItemType(2, R.layout.item_local_article);
        addItemType(3, R.layout.item_collect_dynamic);
    }

    @Override
    protected void convert(BaseViewHolder helper, CollectListBean.DataBeanX item) {

        switch (item.getItemType()) {
            case 1:
                helper.setText(R.id.tv_content, item.getData().getName())
                        .setText(R.id.group_name, item.getData().getCommunity_name())
                        .setText(R.id.pep_num, item.getData().getNum() + "人报名");
                Glide.with(mContext).load(item.getData().getHead_url()).into((ImageView) helper.getView(R.id.item_bg));

                boolean isNotEnd = false;
                String createTime = item.getCreate_time();

                String applyEndTime = MyUtils.timedate(String.valueOf(item.getData().getApply_end_time()));

                isNotEnd = MyUtils.compareTwoTime("", applyEndTime);

                if (isNotEnd) {
                    String leaveTime = MyUtils.getTimeDifference("", applyEndTime);
                    helper.setText(R.id.tv_time, "收藏于" + MyUtils.getTimeFormatText(createTime) + "   距离结束还有" + leaveTime);
                } else {
                    helper.setText(R.id.tv_time, "收藏于" + MyUtils.getTimeFormatText(createTime) + "   报名已截止");
                }


                break;
            case 2:
                String tvTime = MyUtils.getTimeFormatText(item.getCreate_time());
                helper.setText(R.id.tv_title, item.getData().getTitle())
                        .setText(R.id.tv_location, item.getData().getTitle())
                        .setText(R.id.tv_info, tvTime + "   " + item.getData().getOrders() + "人看   " + item.getData().getComment() + "评论");
                Glide.with(mContext).load(item.getData().getHead_url()).into((ImageView) helper.getView(R.id.item_bg));
                break;
            case 3:

                helper.setText(R.id.my_name, item.getData().getDetail().getNickname())
                        .setText(R.id.tv_content, item.getData().getRank()).setText(R.id.from_group, item.getData().getDetail().getCommunity_name())
                        .addOnClickListener(R.id.more);

                if (item.getCreate_time() != null) {
                    helper.setText(R.id.tv_time, MyUtils.getTimeFormatText(item.getCreate_time()) + "   来自");
                } else {
                    helper.setText(R.id.tv_time, "    ");
                }

                Glide.with(mContext).load(item.getData().getDetail().getHead_url()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into((ImageView) helper.getView(R.id.my_head));


                LinearLayout linearLayout = helper.getView(R.id.comment_ryc);
                FlowLayout flowLayout = helper.getView(R.id.flow_ll);
                linearLayout.removeAllViews();
                flowLayout.removeAllViews();

                List<AlbumaBean.DetailBean.ReplyBean> replyBeanList = item.getData().getDetail().getReply();


                if (replyBeanList != null && replyBeanList.size() > 0) {
                    List<AlbumaBean.DetailBean.ReplyBean> replyList = new ArrayList<>();
                    List<AlbumaBean.DetailBean.ReplyBean> praiseList = new ArrayList<>();

                    for (AlbumaBean.DetailBean.ReplyBean data : replyBeanList) {
                        if (data.getType().equals("reply")) {
                            replyList.add(data);
                        } else if (data.getType().equals("dig")) {
                            praiseList.add(data);
                        }
                    }

                    if (replyList.size() > 1) {
                        helper.setGone(R.id.more, true);

                        //全部
                        if (item.getData().isExpand()) {
                            helper.setGone(R.id.more, false);
                            for (AlbumaBean.DetailBean.ReplyBean data : replyList) {

                                TextView tv_comment = new TextView(mContext);
                                String name = data.getNickname() + ":";
                                String conetent = data.getContent();
                                int nameLong = name.length();
                                int contentLong;
                                if (conetent != null) {
                                    contentLong = conetent.length();
                                } else {
                                    contentLong = 0;
                                    conetent = "";
                                }
                                SpannableStringBuilder style = new SpannableStringBuilder(name + conetent);
                                style.setSpan(new ForegroundColorSpan(Color.parseColor("#238cf5")), 0, nameLong, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                                style.setSpan(new ForegroundColorSpan(Color.parseColor("#101010")), nameLong, contentLong + nameLong, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                                tv_comment.setText(style);
                                linearLayout.addView(tv_comment);

                            }
                        } else {
                            //一条评论
                            helper.setGone(R.id.more, true);
                            TextView tv_comment = new TextView(mContext);
                            String name = replyList.get(0).getNickname() + ":";
                            String conetent = replyList.get(0).getContent();
                            int nameLong = name.length();
                            int contentLong;
                            if (conetent != null) {
                                contentLong = conetent.length();
                            } else {
                                contentLong = 0;
                                conetent = "";
                            }
                            SpannableStringBuilder style = new SpannableStringBuilder(name + conetent);
                            style.setSpan(new ForegroundColorSpan(Color.parseColor("#238cf5")), 0, nameLong, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                            style.setSpan(new ForegroundColorSpan(Color.parseColor("#101010")), nameLong, contentLong + nameLong, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                            tv_comment.setText(style);
                            linearLayout.addView(tv_comment);
                        }

                    } else if (replyList.size() == 1) {
                        helper.setGone(R.id.more, false);
                        TextView tv_comment = new TextView(mContext);
                        String name = replyList.get(0).getNickname() + ":";
                        String conetent = replyList.get(0).getContent();
                        int nameLong = name.length();
                        int contentLong;
                        if (conetent != null) {
                            contentLong = conetent.length();
                        } else {
                            contentLong = 0;
                            conetent = "";
                        }
                        SpannableStringBuilder style = new SpannableStringBuilder(name + conetent);
                        style.setSpan(new ForegroundColorSpan(Color.parseColor("#238cf5")), 0, nameLong, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        style.setSpan(new ForegroundColorSpan(Color.parseColor("#101010")), nameLong, contentLong + nameLong, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        tv_comment.setText(style);
                        linearLayout.addView(tv_comment);
                    } else {
                        helper.setGone(R.id.more, false);
                    }

                    if (praiseList.size() > 0) {
                        for (int i = 0; i < praiseList.size(); i++) {
                            AlbumaBean.DetailBean.ReplyBean data = praiseList.get(i);
                            TextView textView = (TextView) LayoutInflater.from(mContext).inflate(R.layout.item_dig, flowLayout, false);
                            if (i == praiseList.size() - 1) {
                                textView.setText(data.getNickname() + ":");
                            } else {
                                textView.setText(data.getNickname() + ",");
                            }
                            flowLayout.addView(textView);
                        }
                        TextView textView = new TextView(mContext);
                        textView.setTextColor(Color.parseColor("#101010"));
                        textView.setText("赞赏了一下");
                        flowLayout.addView(textView);
                    }
                }

                RecyclerView recyclerView = helper.getView(R.id.grid_view);
                recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
                final List<String> stringList = new ArrayList<>();
                if (item.getData().getImages() != null) {
                    String[] strings = item.getData().getImages().split(",");
                    stringList.addAll(Arrays.asList(strings));
                }

                PictureAdapter adapter = new PictureAdapter(R.layout.item_ablum_picture, stringList);
                recyclerView.setAdapter(adapter);

                adapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        Intent intent = new Intent(mContext, PreviewViewpagerActivity.class);
                        Activity activity = (Activity) mContext;
                        intent.putExtra("pos", position);
                        intent.putStringArrayListExtra("list", (ArrayList<String>) stringList);
                        activity.startActivity(intent);
                    }
                });

                break;
        }

    }
}
